<template>
  <div class="container scopClass" >
    <!-- <el-tabs
      v-model="activeName"
      type="border-card"
      style="margin-top: 20px"
      @tab-click="handleClick"
    >
      <el-tab-pane
        :label="item.templateName"
        :name="item.templateName"
        v-for="(item, index) in List"
        :key="index"
      > -->
        <!-- <el-row style="margin: 20px 0">
          <el-col :span="7">
            <el-input
              style="border-radius: 0 5px 5px 0; width: 300px"
              placeholder="请输入关键词"
              v-model.trim="searchName1"
            >
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
              <el-button slot="append" @click="handleSearch1">搜索</el-button>
            </el-input>
          </el-col>
          <el-button
            type="primary"
            @click="newAdd"
            style="float: right; margin-right: 10px"
            >新增</el-button
          >
        </el-row> -->

		<el-row style="margin:20px 0;">
		    <el-col :span="7">
		        <el-input style="border-radius: 0 5px 5px 0;width:300px;" placeholder="请输入关键词"  v-model.trim="searchName1">
		            <i slot="prefix" class="el-input__icon el-icon-search"></i>
		            <el-button slot="append" @click="handleSearch1">搜索</el-button>
		        </el-input>
		    </el-col>



		</el-row>
        <el-table
          :data="tableData"
          border
          ref="multipleTable"
          header-cell-class-name="table-header"
        >
          <el-table-column prop="" sortable label="标识分类" >
            <template slot-scope="scope">
              <span v-if="scope.row.fromFactoryName !== null">{{scope.row.fromFactoryName}}</span>
              <span v-else>{{scope.row.factoryName}}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="factoryName" sortable label="标识分类" ></el-table-column> -->
          <el-table-column v-if='switchHeadTab=="生产系统"' prop="inventoryType" sortable label="产品大类"></el-table-column>
          <el-table-column prop="breedName" sortable label="名称"></el-table-column>
          <!-- <el-table-column prop="loadingQuantity" sortable label="装量"></el-table-column>
          <el-table-column prop="loadingUnits" sortable label="包装"></el-table-column> -->
		  <el-table-column v-if='switchHeadTab=="生产系统"' prop="s" sortable label="药品标识码标准"></el-table-column>
		  <el-table-column prop="prodRecordCode" label="上市备案号"  align="center">

		  </el-table-column>
          <el-table-column prop="createTime" sortable label="添加修改时间"></el-table-column>

         <el-table-column label="操作" width="150" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          layout="total, prev, pager, next, jumper"
          :page-size="pagesize1"
          @current-change="handleCurrentChange1"
          :total="total1"
          :current-page.sync="currentPage1"
          style="margin-top: 20px"
        ></el-pagination>


		<el-dialog  title="编辑上市备案号" :visible.sync="dialogFormVisible1">
		    <el-form :model="ruleForm1" :rules="rules1" ref="ruleForm1">
		        <el-row>
		            <el-col :span="16">

		                <el-form-item label="上市备案号：" prop="prodRecordCode" label-width="120px">
		                    <el-input v-model.trim="ruleForm1.prodRecordCode" style="width:300px">
		                    </el-input>
		                </el-form-item>
		            </el-col>
		        </el-row>
		    </el-form>
		    <div slot="footer" class="dialog-footer">
		        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
		        <el-button type="primary" @click="submitForm1('ruleForm1')">确 定</el-button>
		    </div>
		</el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { materialBreed_list, materialBreed_delete,materialBreed_save } from "@/request/api";
import {
  materials_import,
  template_findByCompanyId as templateFindByCompanyId,
  materialBreed_findByLis as materialBreedFindByLis,
} from "@/request/manageapi";
import uploadFile from "../../../common/uploadfile";
import { mapState } from 'vuex';
export default {
  props: ["activeName1"],
  components: {
    uploadFile,
  },
  data() {
    return {
      tableData: [],
      total1: 0,
      currentPage1: 1,
      pagesize1: 10,
      activeName: "",
      searchName1: "",
      List: [],
	  dialogFormVisible1:false,
	  ruleForm1:{},
	  rules1:{
		  prodRecordCode: [
			  { required: true, message: '不能为空', trigger: 'change' }
		  ],
	  }
    };
  },
  async created() {
        await this.init();
        await this.materialBreedFindByLis();
    // var data = JSON.stringify(this.$store.state.wl);
    // var a = "新增";
    // var b = "编辑";
  },
    computed:{
      ...mapState(['switchHeadTab'])
    },
  destroyed: function () {
    this.$store.commit("setStorName", "");
  },
  methods: {
	  submitForm1(formName) {
	      this.$refs[formName].validate((valid) => {
	          if (valid) {
				this.ruleForm1.createTime = ""
				  materialBreed_save(this.ruleForm1).then((res) => {
					  this.$notify({
						  title: '提示',
						  message: '编辑成功',
						  type: 'success',
						  duration:1000
					  });
					  this.dialogFormVisible1 = false
					  this.init()
				  })

	          } else {
	              return false;
	          }
	      })
	  },
	  // 打开销售备案号弹窗
	  handleCheck(index,row){
		  this.dialogFormVisible1 = true
		  if(row.sellRecord){
			  this.tableData1 = JSON.parse(row.sellRecord)
		  }

	  },
    // 调用接口
    init() {
      // let companyId = this.activeName1;
      let companyId = localStorage.getItem("companyId")
      return new Promise((resolve, reject) => {
        templateFindByCompanyId(`?companyId=${companyId}&&isNew=1`)
          .then((res) => {
            this.List = res.data;
            if (this.List.length > 0) {
              this.activeName = this.List[0].templateName;
            }
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    materialBreedFindByLis() {

      let companyId = this.activeName1;
      let materialType = this.switchHeadTab == "生产系统"? "配方颗粒":"中药饮片";
      let inventoryType = "C60中药配方颗粒,C70中药配方颗粒";
      let page = this.currentPage1;
	  let searchName = this.searchName1
      let data = {
        companyId,
        materialType,
        inventoryType,
        page,
		searchName
      };
      return new Promise((resolve, reject) => {
        materialBreedFindByLis(data).then((res) => {
          if (res.code == 200) {
            this.total1 = res.total
            let tableData = res.rows;
            this.tableData = JSON.parse(JSON.stringify(tableData));
			for(var i=0;i<this.tableData.length;i++){
				if(this.tableData[i].ypCodeStandard == 1){
					this.tableData[i].s = "国标"
				}else if(this.tableData[i].ypCodeStandard == 2){
					this.tableData[i].s = "省标"
				}else if(this.tableData[i].ypCodeStandard == 3){
					this.tableData[i].s = "省标-北京"
				}else if(this.tableData[i].ypCodeStandard == 4){
					this.tableData[i].s = "省标-上海"
				}
			}
          }
          resolve(res);
        });
      });
    },
    handleSearch1() {
		this.page = 1
		this.total1 = 0
		this.materialBreedFindByLis()
	},
    // tabs
    async handleClick(tab, event) {
      this.currentPage1 = 1
      await this.materialBreedFindByLis();
    },
    // 编辑
    handleEdit(index, row) {
      this.dialogFormVisible1 = true
	  this.ruleForm1 = row
    },
    // 分页事件
    async handleCurrentChange1(val) {
      this.currentPage1 = val;
      await this.materialBreedFindByLis();
    },
  },
  watch: {
    activeName1: {
      async handler(newVal, oldVal) {
        this.activeName1 = newVal;
        await this.init();
        await this.materialBreedFindByLis();
      },
      deep: true,
    },
  },
};
</script>

<style lang="less" scoped>
// 深度作用器
/deep/.upload-daoru {
  .el-upload {
    border: none;
    width: auto;
    height: auto;
    border-radius: 0;
    float: right;
    margin-right: 10px;
  }
}
.el-col {
  margin-bottom: 0;
}
/* .scopClass .el-table{ margin-top: 20px;} */
.el-pagination {
  text-align: center;
}
/*
.el-select .el-input {
width: 130px;
}
.input-with-select .el-input-group__prepend {
background-color: #fff;
}
.el-input--small .el-input__inner{
    border-radius: 5px 0 0 5px !important;
    border-right: none;
} */
</style>
